package com.zjuee.web.tporder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.bean.response.CommonResultCode;
import com.zjuee.base.constant.DictionaryConstants;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.exception.BaseException;
import com.zjuee.base.utils.DesensitizationUtil;
import com.zjuee.core.translation.service.TranslateData;
import com.zjuee.service.TransOrderApplyService;
import com.zjuee.base.dto.common.tporder.TransOrderApplyDto;
import com.zjuee.base.model.common.tporder.WkTransOrderApplyEntity;
import com.zjuee.base.model.common.tporder.WkTransOrderCompanyEntity;
import com.zjuee.base.model.common.tporder.WkTransOrderMaterialEntity;
import com.zjuee.base.model.common.tporder.WkTransStatisticsEntity;
import com.zjuee.vo.WkTransOrderApplyVo;
import com.zjuee.vo.WkTransOrderCompanyVo;
import com.zjuee.vo.WkTransOrderMaterialVo;
import com.zjuee.web.tporder.mapper.TransOrderApplyMapper;
import com.zjuee.web.tporder.mapper.TransOrderCompanyMapper;
import com.zjuee.web.tporder.mapper.TransOrderMaterialMapper;
import com.zjuee.web.tporder.mapper.TransStatisticsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TransOrderApplyImpl implements TransOrderApplyService {
    @Resource
    private TransStatisticsMapper transStatisticsMapper;
    @Resource
    private TransOrderApplyMapper transOrderApplyMapper;
    @Resource
    private TransOrderCompanyMapper transOrderCompanyMapper;
    @Resource
    private TransOrderMaterialMapper transOrderMaterialMapper;
    @Resource
    private TranslateData translateData;
    @Resource
    private LoginDto loginDto;
    @Override
    public Long add(WkTransOrderApplyVo type) {
        WkTransOrderApplyEntity wkTransOrderApplyEntity = new WkTransOrderApplyEntity();
        BeanUtils.copyProperties(type, wkTransOrderApplyEntity);
        transOrderApplyMapper.insert(wkTransOrderApplyEntity);
        List<WkTransOrderCompanyVo> wkTransOrderCompanyVos = type.getWkTransOrderCompanyVos();
        for(WkTransOrderCompanyVo wkTransOrderCompanyVo : wkTransOrderCompanyVos){
            WkTransOrderCompanyEntity wkTransOrderCompanyEntity = new WkTransOrderCompanyEntity();
            BeanUtils.copyProperties(wkTransOrderCompanyVo, wkTransOrderCompanyEntity);
            wkTransOrderCompanyEntity.setWtoaId(wkTransOrderApplyEntity.getId());
            transOrderCompanyMapper.insert(wkTransOrderCompanyEntity);
        }

        List<WkTransOrderMaterialVo> wkTransOrderMaterialVos = type.getWkTransOrderMaterialVos();
        for(WkTransOrderMaterialVo wkTransOrderMaterialVo : wkTransOrderMaterialVos){
            WkTransOrderMaterialEntity wkTransOrderMaterialEntity = new WkTransOrderMaterialEntity();
            BeanUtils.copyProperties(wkTransOrderMaterialVo, wkTransOrderMaterialEntity);
            wkTransOrderMaterialEntity.setWtoaId(wkTransOrderApplyEntity.getId());
            transOrderMaterialMapper.insert(wkTransOrderMaterialEntity);
        }

        //总览表更新转出联单数量和废物量
        QueryWrapper<WkTransStatisticsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wts_district_code", 3);
        queryWrapper.eq("wts_parent_code", 3333);
        queryWrapper.eq("wts_type", 3);
        WkTransStatisticsEntity wkTransStatisticsEntity = transStatisticsMapper.selectOne(queryWrapper);
        UpdateWrapper<WkTransStatisticsEntity> updateWrapper1 = new UpdateWrapper<>();
        wkTransStatisticsEntity.setWtsTransferOutNum(wkTransStatisticsEntity.getWtsTransferOutNum().add(new BigDecimal("1")));
        for(WkTransOrderMaterialVo wkTransOrderMaterialVo : wkTransOrderMaterialVos){
            wkTransStatisticsEntity.setWtsTransferOut(wkTransStatisticsEntity.getWtsTransferOut().add(wkTransOrderMaterialVo.getWtomZysl()));
        }
        updateWrapper1.eq("id", wkTransStatisticsEntity.getId());
        transStatisticsMapper.update(wkTransStatisticsEntity, updateWrapper1);

        return wkTransOrderApplyEntity.getId();

    }

    @Override
    public boolean delete(Long id, Long validateId) {
        return false;
    }

    @Override
    public boolean update(WkTransOrderApplyVo type) {
        return false;
    }

    @Override
    public WkTransOrderApplyVo selectById(Long id, Long validateId) {
        return null;
    }

    @Override
    public Page<WkTransOrderApplyVo> list(TransOrderApplyDto dto) {
        Long orgId = loginDto.getOrgBusinessId();
        if (orgId == null) {
            throw new BaseException(CommonResultCode.AUTH_EXP);
        }
        dto.setBciId(orgId);
        Page<WkTransOrderApplyVo> page = transOrderApplyMapper.getTransOrderApplyList(dto.getPage().convert(), dto);
        List<Long> list = page.getRecords().stream().map(WkTransOrderApplyVo::getId).collect(Collectors.toList());
        if (list.isEmpty()){
            return page;
        }
        QueryWrapper<WkTransOrderMaterialEntity> wrapper = new QueryWrapper<>();
        wrapper.in("wtoa_id",list).eq("WTOM_TYPE", DictionaryConstants.TRANS_ORDER_OUT);
        List<WkTransOrderMaterialEntity> materialEntityList = transOrderMaterialMapper.selectList(wrapper);
        for (WkTransOrderApplyVo applyVo : page.getRecords()) {
            List<WkTransOrderMaterialEntity> materialEntities = materialEntityList.stream().filter(m -> m.getWtoaId().equals(applyVo.getId())).collect(Collectors.toList());
            applyVo.setWtoaReceive(materialEntities.get(0).getWtomJssl());
            applyVo.setWtoaCarry(materialEntities.get(0).getWtomZysl());
            applyVo.setWtoaScode(materialEntities.get(0).getWtomFwdm());
            applyVo.setWtoaName(materialEntities.get(0).getWtomFwmc());

            applyVo.setWtoaWfycrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(applyVo.getWtoaWfycrq()));
            if (applyVo.getWtoaWfjsdwjsrq() != null){
                applyVo.setWtoaWfjsdwjsrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(applyVo.getWtoaWfjsdwjsrq()));
            }
            applyVo.setWtoaWfycdwlxr(DesensitizationUtil.desensitizedName(applyVo.getWtoaWfycdwlxr()));
            applyVo.setWtoaFwycdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(applyVo.getWtoaFwycdwlxrsj()));
            applyVo.setWtoaWfjsdwlxr(DesensitizationUtil.desensitizedName(applyVo.getWtoaWfjsdwlxr()));
            applyVo.setWtoaWfjsdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(applyVo.getWtoaWfjsdwlxrsj()));
        }
        return page;
    }

    @Override
    public LambdaQueryWrapper<WkTransOrderApplyVo> fuzzyQuery(TransOrderApplyDto dto) {
        return null;
    }

    @Override
    public WkTransOrderApplyVo getTransOrderApply(Long id) {
        WkTransOrderApplyEntity wkTransOrderApplyEntity = transOrderApplyMapper.selectById(id);

        QueryWrapper<WkTransOrderCompanyEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("wtoa_id", id);
        List<WkTransOrderCompanyEntity> wkTransOrderCompanyEntitys = transOrderCompanyMapper.selectList(queryWrapper1);

        QueryWrapper<WkTransOrderMaterialEntity> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("wtoa_id", id);
        List<WkTransOrderMaterialEntity> wkTransOrderMaterialEntitys = transOrderMaterialMapper.selectList(queryWrapper2);


        WkTransOrderApplyVo wkTransOrderApplyVo =  new WkTransOrderApplyVo();

        List<WkTransOrderCompanyVo> wkTransOrderCompanyVos = new ArrayList();

        List<WkTransOrderMaterialVo> wkTransOrderMaterialVos = new ArrayList();

        BeanUtils.copyProperties(wkTransOrderApplyEntity, wkTransOrderApplyVo);
        for(WkTransOrderCompanyEntity wkTransOrderCompanyEntity : wkTransOrderCompanyEntitys){
            WkTransOrderCompanyVo wkTransOrderCompanyVo = new WkTransOrderCompanyVo();
            BeanUtils.copyProperties(wkTransOrderCompanyEntity, wkTransOrderCompanyVo);
            wkTransOrderCompanyVos.add(wkTransOrderCompanyVo);
        }
        for(WkTransOrderMaterialEntity wkTransOrderMaterialEntity : wkTransOrderMaterialEntitys){
            WkTransOrderMaterialVo wkTransOrderMaterialVo = new WkTransOrderMaterialVo();
            BeanUtils.copyProperties(wkTransOrderMaterialEntity, wkTransOrderMaterialVo);
            translateData.translateData(wkTransOrderMaterialVo,"transOrderHandle",
                    DictionaryConstants.PB_WASTE_SHAPE,
                    DictionaryConstants.PB_WASTE_PACK_TYPE
//                    RedisCacheType.DISPOSE_STYPE.toString()
            );
            wkTransOrderMaterialVos.add(wkTransOrderMaterialVo);
        }

        wkTransOrderApplyVo.setWtoaWfycrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(wkTransOrderApplyVo.getWtoaWfycrq()));
        if (wkTransOrderApplyVo.getWtoaWfjsdwjsrq() != null){
            wkTransOrderApplyVo.setWtoaWfjsdwjsrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(wkTransOrderApplyVo.getWtoaWfjsdwjsrq()));
        }
        wkTransOrderApplyVo.setWkTransOrderCompanyVos(wkTransOrderCompanyVos);
        wkTransOrderApplyVo.setWkTransOrderMaterialVos(wkTransOrderMaterialVos);
        wkTransOrderApplyVo.setWtoaWfycdwlxr(DesensitizationUtil.desensitizedName(wkTransOrderApplyVo.getWtoaWfycdwlxr()));
        wkTransOrderApplyVo.setWtoaFwycdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(wkTransOrderApplyVo.getWtoaFwycdwlxrsj()));
        wkTransOrderApplyVo.setWtoaWfjsdwlxr(DesensitizationUtil.desensitizedName(wkTransOrderApplyVo.getWtoaWfjsdwlxr()));
        wkTransOrderApplyVo.setWtoaWfjsdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(wkTransOrderApplyVo.getWtoaWfjsdwlxrsj()));
        return wkTransOrderApplyVo;
    }
}
